package gttp.nspa.shared.om;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.condition.IfNotNull;

@Entity(name = "MSOD")
public class MassaOrder implements IOrder, IsSerializable {

    @Id
    private Long   id;

    @Index
    private long   time   = System.currentTimeMillis();

    private Long   timeServe;

    private Long   timeEnd;

    @Index(IfNotNull.class)
    private String massa;

    private int    income;

    @Index(IfNotNull.class)
    private String therapist;

    @Index
    private Status status = Status.ORDERING;

    @Index(IfNotNull.class)
    private String reason;

    public MassaOrder(String therapist) {
        this(therapist, null, 0);
    }

    public MassaOrder(String therapist, String massa, int income) {
        this.therapist = therapist;
        this.massa = massa;
        this.income = income;
    }

    public Long getId() {
        return id;
    }

    public long getTime() {
        return time;
    }

    public Long getTimeEnd() {
        return timeEnd;
    }

    public Long getTimeServe() {
        return timeServe;
    }

    public String getProduct() {
        return getMassa();
    }

    public int getIncome() {
        return income;
    }

    public void setIncome(int income) {
        this.income = income;
    }

    public Status getStatus() {
        return null;
    }

    public String getMassa() {
        return massa;
    }

    public void setMassa(String massa) {
        this.massa = massa;
    }

    public String getTherapist() {
        return therapist;
    }

    public void setTherapist(String therapist) {
        this.therapist = therapist;
    }

    public String getReason() {
        return reason;
    }

    public boolean cancel(String reason) {
        if (status == Status.ORDERING) {
            status = Status.CANCELLED;
            this.reason = reason;
            timeEnd = System.currentTimeMillis();
            return true;
        }
        return false;
    }

    public boolean serve() {
        if (status == Status.ORDERING) {
            status = Status.SERVING;
            timeServe = System.currentTimeMillis();
            return true;
        }
        return false;
    }

    public boolean serveDone() {
        if (status == Status.ORDERING || status == Status.SERVING) {
            status = Status.SERVED;
            timeEnd = System.currentTimeMillis();
            return true;
        }
        return false;
    }
}
